package com.link2room.rodimus.facade.impl;

import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.common.util.StringUtil;
import com.link2room.rodimus.entity.MemberBase;
import com.link2room.rodimus.exception.MemberException;
import com.link2room.rodimus.constant.MemberConstant;
import com.link2room.rodimus.dto.param.base.MemberBaseDto;
import com.link2room.rodimus.dto.param.inside.CreateMemberByMobileParamDto;
import com.link2room.rodimus.dto.param.inside.MemberBaseInsideParamDto;
import com.link2room.rodimus.facade.IMemberInsideFacadeService;
import com.link2room.rodimus.service.IMemberService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by LN on 2017/6/21.10:50
 */
@Component("memberInsideFacadeService")
public class MemberInsideFacadeServiceImpl implements IMemberInsideFacadeService {
    @Resource
    private ICommonService commonService;
    @Resource
    private IMemberService memberService;
    @Override
    public MemberBaseDto selectMemberBase(MemberBaseInsideParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode,"[lhotelGroupCode] is null.");
        String openid = paramDto.getOpenid();
        String mobile = paramDto.getMobile();
        if(StringUtil.isBlank(openid) && StringUtil.isBlank(mobile)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(),
                    BaseSysExceptionEnum.INVALID_PARA.getMsg()+":[openid] 和 [mobile] 不能同时为空",
                    BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }

        MemberBase find = new MemberBase();
        find.setOpenid(openid);
        find.setMobile(mobile);
        find.setLhotelGroupCode(lhotelGroupCode);
        MemberBase qres = commonService.findOneEQ(MemberBase.class, find);
        if(qres == null){
            return null;
        }else{
            return ClassConverUtil.copyProperties(qres,MemberBaseDto.class);
        }
    }

    @Override
    public void checkAndCreateNewMemberByMobileIfNotExit(CreateMemberByMobileParamDto paramDto) throws Exception {
        String mobile = paramDto.getMobile();
        L2RAssert.stringBlank(mobile,"[mobile] is null.");
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode,"[lhotelGroupCode] is null.");
        if(!memberService.hasMemberByMobile(lhotelGroupCode,mobile)){
            //不重复新建
            MemberBase memberBase = new MemberBase();
            memberBase.setMemberName("");
            memberBase.setMobile(mobile);
            memberBase.setLhotelGroupCode(lhotelGroupCode);
            memberBase.setMemberLevel(MemberConstant.MEMBER_LEVEL.L0);
            memberBase.setOpenid("");
            String memberNo = memberService.syncGetNewMemberNo();
            if(StringUtil.isBlank(memberNo)){
                throw new MemberException(BaseSysExceptionEnum.BASE_FUCATION_OPTION,"memberNo get Error,");
            }
            memberBase.setMemberNo(memberNo);
            commonService.save(memberBase);
        }
    }


}
